/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.finance.controller.mini;


import com.medusa.gruul.common.core.util.PageUtils;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.finance.api.entity.FinanceWithdrawalRecord;
import com.medusa.gruul.finance.api.enums.AccountTypeEnum;
import com.medusa.gruul.finance.model.ApiFinanceAccountVo;
import com.medusa.gruul.finance.model.ApiWithdrawalBrokerageDto;
import com.medusa.gruul.finance.model.ApiWithdrawalRecordDto;
import com.medusa.gruul.finance.service.IFinanceAccountService;
import com.medusa.gruul.finance.service.IFinanceWithdrawalRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 资金账户 前端控制器
 * </p>
 *
 * @author alan
 * @since 2020 -02-22
 */
@Slf4j
@RestController
@RequestMapping("/mini")
@Api(tags = "小程序端财务模块")
public class MiniFinanceController {
    @Resource
    private IFinanceAccountService financeAccountService;
    @Resource
    private IFinanceWithdrawalRecordService financeWithdrawalRecordService;


    /**
     * Gets account info.
     *
     * @param accountType the account type
     * @param userId      the user id
     * @return the account info
     */
    @ApiOperation("用户概况")
    @GetMapping("/account/info")
    public Result<ApiFinanceAccountVo> getAccountInfo(
            @ApiParam(name = "accountType", value = "用户类型") @RequestParam(value = "accountType") AccountTypeEnum accountType,
            @ApiParam(name = "userId", value = "对应的ID", example = "0") @RequestParam(value = "userId") String userId) {
        ApiFinanceAccountVo vo = financeAccountService.getFinanceAccountVo(accountType, userId);
        return Result.ok(vo);
    }


    /**
     * Last withdrawal result.
     *
     * @param accountType the account type
     * @param userId      the user id
     * @return the result
     */
    @ApiOperation("获取最后一次申请的数据")
    @GetMapping("/withdrawal/last")
    public Result lastWithdrawal(
            @ApiParam(name = "accountType", value = "用户类型") @RequestParam(value = "accountType") AccountTypeEnum accountType,
            @ApiParam(name = "userId", value = "对应的ID", example = "0") @RequestParam(value = "userId") String userId) {
        FinanceWithdrawalRecord record = financeWithdrawalRecordService.getLastWithdrawal(accountType, userId);
        return Result.ok(record);
    }

    /**
     * Withdrawal result.
     *
     * @param dto   the dto
     * @param token the token
     * @return the result
     */
    @ApiOperation("申请提现")
    @PostMapping("/withdrawal")
    public Result withdrawal(@RequestBody @Validated ApiWithdrawalBrokerageDto dto, @RequestHeader String token) {
        financeWithdrawalRecordService.withdrawal(dto, token);
        return Result.ok();
    }


    /**
     * Gets withdrawal record.
     *
     * @param dto the dto
     * @return the withdrawal record
     */
    @ApiOperation("申请提现的列表")
    @GetMapping("/withdrawal")
    public Result<PageUtils<FinanceWithdrawalRecord>> getWithdrawalRecord(ApiWithdrawalRecordDto dto) {
        PageUtils pageUtils = financeWithdrawalRecordService.getWithdrawalRecord(dto);
        return Result.ok(pageUtils);
    }


}
